package com.hb.trade.server.project.order.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hb.core.pojo.Attachments;
import com.hb.core.pojo.JSONObjectProperty;
import com.hb.core.pojo.enums.PayStatusEnum;
import com.hb.core.pojo.project.order.dto.UserOrderDTO;
import com.hb.core.pojo.project.order.entity.UserOrder;
import com.hb.core.pojo.project.order.entity.UserOrderGoods;
import com.hb.trade.server.project.order.service.UserOrderService;
import org.fzzn.core.model.enums.AuditStatusEnum;

import java.math.BigDecimal;
import java.util.List;

/**
 * describe: 订单对象业务操作
 *
 * @author zhongdl
 * @update zhaojk
 * @date 2021/12/22
 */
public class UserOrderManager extends UserOrderDTO {

    /**
     * 填充订单基础数据
     * 微信支付宝拉起第三方支付的, 默认审核状态是已通过, 支付状态是未支付
     * 静态收款码支付的默认审核状态是未审核, 支付状态是已通过
     * 最终支付成功的订单必须是审核状态是已通过, 并且支付状态是已支付的
     *
     * @author zhaojk
     */
    public void init() {
        Attachments attachments = getAttachment();
        // 设置订单支付, 审核的默认状态
        if (CollUtil.isEmpty(attachments)) { // 如果付款凭证不存在, 则认为这是第三方支付
            String outOrderNo = getOutOrderNo();

            // 之前已经有的订单需要更新 start
            UserOrderService orderService = SpringUtil.getBean(UserOrderService.class);
            UserOrderDTO params = new UserOrderDTO();
            params.setOutOrderNo(outOrderNo);
            UserOrder userOrder = orderService.getUserOrder(params);
            // 之前已经有的订单需要更新 end

            if (userOrder != null) {
                // 设置ID返回上层逻辑时会执行更新操作
                setId(userOrder.getId());
            }
            setOrderStatus(PayStatusEnum.UN_PAY.getCode());
            setAuditStatus(AuditStatusEnum.PASSED.getCode());
        } else {
            setOrderStatus(PayStatusEnum.PAY_SUCCESS.getCode());
            setAuditStatus(AuditStatusEnum.WAITING.getCode());
        }

        // 设置订单金额(商品列表总价的和)
        List<UserOrderGoods> userOrderGoods = generateOrderGoods();
        if (CollUtil.isEmpty(userOrderGoods)) {
            return;
        }

        // 订单总价(根据各商品价格求和)
        BigDecimal sum = BigDecimal.ZERO;

        for (UserOrderGoods userOrderGood : userOrderGoods) {
            BigDecimal goodPrice = userOrderGood.getGoodPrice();
            if (goodPrice == null) {
                continue;
            }
            sum = sum.add(goodPrice);
        }

        setOrderAmount(sum);
        setActualAmount(sum);
    }

    /**
     * 构建商品明细信息
     *
     * @author zhaojk
     */
    public List<UserOrderGoods> generateOrderGoods() {
        // 获取商品列表
        List<UserOrderGoods> goods = getGoods();
        if (CollUtil.isEmpty(goods)) {
            return null;
        }

        UserOrderService userOrderService = SpringUtil.getBean(UserOrderService.class);

        for (UserOrderGoods good : goods) {
            good.setOrderId(getId());

            JSONObjectProperty jsonEntity = new JSONObjectProperty();
            jsonEntity.putOpt("sectionId", getSectionId());
            jsonEntity.putOpt("fileId", getFileId());

            // 获取指定标段的相关业务价格
            BigDecimal price = userOrderService.getPrice(getSectionId(), good.getFeeType());

            good.setGoodPrice(price);
            good.buildBaseData(1);

            // 保存标段信息
            good.setDataInfo(jsonEntity);
        }
        setGoods(goods);
        return goods;
    }


}
